home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_100
/
160_01
/
bj
< prev
next >
Wrap
Text File
|
1985-11-24
|
15KB
|
695 lines
###makefile.
FILES=bj.c error.c dekmgr.c \
getbet.c hndmgr.c nfrom.c outcom.c query.c takes.c
OBJECTS=bj.o error.o dekmgr.o \
getbet.o hndmgr.o nfrom.o outcom.o query.o takes.o
DEFS=local.h bj.h dekmgr.h hndmgr.h ttymgr.h
LINT=lint -phbxac
cleanup:
-rm *.o
-du
install:
size bj /a1/phi/bin/bj
cp bj /a1/phi/bin/bj; rm bj
print: Makefile $(DEFS) $(FILES)
pr $?
date >print
bj: $(DEFS) $(OBJECTS)
cc -o bj $(OBJECTS)
bj.lint: $(DEFS) $(FILES)
$(LINT) >bj.lint $(FILES)
outcom.x: $(DEFS) outcom.c error.o
cc -o outcom.x -DTRYMAIN outcom.c error.o
all:
make bj
make bj.lint
make install
###bj.c
/* bj - blackjack
* Permission is hereby granted to reproduce and use bj
*/
#include "local.h"
#include "bj.h"
#include "dekmgr.h"
#include "hndmgr.h"
#include "ttymgr.h"
main()
{
CASH action; /* how much money has crossed the table */
CASH bet; /* amount of player's current bet per hand */
CASH result; /* net result of this hand, plus or minus */
CASH standing; /* how much has player won or lost */
bool canhit; /* can player's hand take hit? */
bool isdbl; /* did player take DBLDN? */
bool isinsur; /* did player take insurance? */
short hand; /* current hand number */
short reply; /* player's reply to DBLDN, SPLIT */
short tophand; /* how many hands is player playing, 1 or 2 */
printf("Copyright (c) Plum Hall Inc, 1983\n");
/* permission to copy and modify is granted, provided that
* this printout and comment remain intact
*/
printf("\nWelcome to the Blackjack table\n");
action = standing = 0;
opndek();
while ((bet = getbet()) != 0)
{
tophand = 1;
isinsur = isdbl = NO;
if (deklow())
shuffl();
deal();
if (val(DEALER, 0) == 11)
isinsur = takes("i");
reply = query();
if (reply == SPLIT)
tophand = split();
else if (reply == DBLDN)
{
hit(1);
printf("\n");
isdbl = YES;
bet *= 2;
}
for (hand = 1; hand <= tophand; ++hand)
{
if (tophand == 2)
printf("Hand %d:\n", hand);
canhit = !isdbl;
canhit &= !isbj(1);
canhit &= (reply != SPLIT || val(1, 0) != 11);
while (canhit && takes("h"))
{
canhit = hit(hand);
printf("\n");
}
if (21 < score(hand))
printf("Bust\n");
}
printf("Dealer has ");
show(DEALER, 0);
printf(" + ");
show(DEALER, 1);
if (!allbst())
while (score(DEALER) < 17)
hit(DEALER);
printf(" = %d\n", score(DEALER));
result = outcom(bet, tophand, isinsur, isdbl);
action += ABS(result);
standing += result;
printf("action = ");
printf(CASHOUT, action);
printf(" standing = ");
printf(CASHOUT, standing);
printf("\n");
}
printf("\nThanks for the game.\n");
exit(SUCCEED);
}
###bj.h
/* bj.h - include-file for blackjack
*/
/* defined types
*/
#define CASH long /* dollars */
/* defined constants
*/
#define DEALER 0 /* which hand is dealer; not modifiable */
#define NONE 0 /* no reply */
#define DBLDN 1 /* reply: double down */
#define SPLIT 2 /* reply: split pair */
#define INSUR 3 /* takes: insurance */
#define HIT 4 /* takes: hit */
#define CASHIN "%ld" /* input format for CASH data */
#define CASHOUT "%ld" /* output format for CASH data */
###dekmgr.c
/* dekmgr - deck manager
*/
#include "local.h"
#include "bj.h"
#include "dekmgr.h"
#define NCARDS 4 * 52
static short deck[NCARDS] = 0; /* the deck */
static short nc = 0; /* subscript of next card */
static short shufpt = 0; /* subscript of shuffle point */
/* deklow - is deck at or past shuffle point?
*/
bool deklow()
{
return (shufpt <= nc);
}
/* opndek - initialize the deck
*/
void opndek()
{
short i;
short low;
short varnum();
for (low = 0; low < NCARDS; low += 52)
for (i = 0; i < 52; ++i)
deck[i + low] = i;
srand(varnum());
shuffl();
}
/* shuffl - shuffle the deck
*/
void shuffl()
{
short t; /* temporary for swap */
short i; /* index for loop over cards */
short j; /* index for swap */
short nfrom(); /* fn to produce random number */
for (i = 0; i < NCARDS - 1; ++i)
{
j = nfrom(i, NCARDS - 1);
t = deck[j], deck[j] = deck[i], deck[i] = t;
}
shufpt = nfrom(NCARDS - 52, NCARDS - 36);
nc = 0;
printf("Shuffle\n");
}
/* tkcard - take a card
*/
short tkcard()
{
if (NCARDS <= nc)
shuffl();
return (deck[nc++]);
}
/* varnum - return a varying startoff number
*/
short varnum()
{
long time(); /* SYSTEM DEPENDENT - NEEDS CLOCK */
return ((short)time(0));
}
###dekmgr.h
/* dekmgr.h - interface for deck manager
*/
bool deklow();
void opndek();
void shuffl();
short tkcard();
###error.c
/* error - print fatal error message
*/
#include "local.h"
void error(s1, s2)
char s1[], s2[];
{
write(STDERR, s1, strlen(s1));
write(STDERR, " ", 1);
write(STDERR, s2, strlen(s2));
write(STDERR, "\n", 1);
exit(FAIL);
}
###getbet.c
/* getbet - get the player's bet
*/
#include "local.h"
#include "bj.h"
#define MINBET 2
#define MAXBET 1000
CASH getbet()
{
char line[BUFSIZ]; /* input line */
short retn; /* return from getln and sscanf */
CASH bet; /* player's bet */
printf("\n\nYour bet (amount): ");
FOREVER
{
retn = getln(line, BUFSIZ);
if (retn == EOF)
return (0);
retn = sscanf(line, CASHIN, &bet);
if (retn != 1 || bet < MINBET || MAXBET < bet)
printf("Number from %d to %d please: ",
MINBET, MAXBET);
else
return (bet);
}
}
###hndmgr.c
/* hndmgr - hand manager
*/
#include "local.h"
#include "bj.h"
#include "hndmgr.h"
#include "dekmgr.h"
static char spots[13][3] =
{"A", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "J", "Q", "K"};
static char suits[4][2] = {"S", "H", "D", "C"};
static short hands[3][12] = 0; /* three hands */
static short ncards[3] = 0; /* how many cards in each hand */
static short tophand = 0; /* how many player hands active */
/* allbst - are all player's hands busted?
*/
bool allbst()
{
if (score(1) <= 21 || (tophand == 2 && score(2) <= 21))
return (NO);
else
return (YES);
}
/* deal - initialize the hands
*/
void deal()
{
hands[1][0] = tkcard();
hands[DEALER][0] = tkcard();
hands[1][1] = tkcard();
hands[DEALER][1] =tkcard();
ncards[DEALER] = ncards[1] = 2;
tophand = 1;
printf("The dealer shows ");
show(DEALER, 0);
printf("\nYou have ");
show(1, 0);
printf(" + ");
show(1, 1);
printf("\n");
}
/* hit - add a card to a hand
*/
bool hit(h)
short h; /* which hand */
{
hands[h][ncards[h]] = tkcard();
printf(" + ");
show(h, ncards[h]);
++ncards[h];
if (21 < score(h) || h == DEALER && 17 <= score(h))
return (NO);
else
return (YES);
}
/* isbj - is hand a "natural" 2-card blackjack?
*/
bool isbj(h)
short h; /* which hand */
{
if (h == DEALER)
return (ncards[DEALER] == 2 && score(DEALER) == 21);
else if (h == 1)
return (tophand == 1 && ncards[1] == 2 && score(1) == 21);
else
return (NO);
}
/* score - tell blackjack value of hand
*/
short score(h)
short h; /* which hand */
{
short aces = 0; /* number of aces in hand */
short i; /* card counter */
short sum = 0; /* accumulated value of hand */
for (i = 0; i < ncards[h]; ++i)
{
sum += val(h, i);
if (val(h, i) == 11)
++aces;
}
for (i = aces; 0 < i; --i)
if (21 < sum)
sum -= 10;
return (sum);
}
/* show - print a card
*/
void show(h, i)
short h; /* which hand */
short i; /* which card */
{
printf("%s", spots[hands[h][i] % 13]);
printf("%s", suits[hands[h][i] / 13]);
}
/* split - split the players pair if allowed
*/
short split()
{
if (val(1, 0) != val(1, 1))
return (1);
hands[2][0] = hands[1][1];
hands[1][1] = tkcard();
hands[2][1] = tkcard();
ncards[2] = 2;
printf("Hand 1: "); show(1, 0); printf(" + "); show(1, 1);
printf("\n");
printf("Hand 2: "); show(2, 0); printf(" + "); show(2, 1);
printf("\n");
tophand = 2;
return (2);
}
/* val - tell value of card n of hand h
*/
short val(h, i)
short